using System;
using System.Collections;
using UnityEngine;

public class Grenade : MonoBehaviour
{
	public AttackInfo attackInfo;

	public Rigidbody2D rb;

	public Transform tf;

	public Transform TF_Shadow;

	public SpriteRenderer sr;

	public SpriteRenderer SR_ExplosionZone;

	public int expolosionRange;

	public void Init(Vector2 throwingPow)
	{
		attackInfo.power = 500;
		rb.velocity = throwingPow;
		SR_ExplosionZone.color = new Color(SR_ExplosionZone.color.r, SR_ExplosionZone.color.g, SR_ExplosionZone.color.b, 0f);
		SR_ExplosionZone.size = new Vector2(expolosionRange * 2, expolosionRange * 2);
		StartCoroutine(ChangeColor());
		StartCoroutine(IE_DrawShadow());
		StartCoroutine(Timer());
	}

	private IEnumerator Timer()
	{
		yield return new WaitForSeconds(3.5f);
		Generator.instance.SpawnObj(4, new Vector2(tf.position.x, tf.position.y + 1.8f));
		Audio.instance.sfx[8].Play();
		Generator.instance.SpawnObj(5, new Vector2(tf.position.x, 0f));
		Generator.instance.SpawnObj(6, new Vector2(tf.position.x, 1f));
		Cam.instance.Shake(0.1f, 0.8f);
		try
		{
			float num = Vector2.Distance(tf.position, Player.instance.tf.position);
			if (num < Player.instance.hitSize + (float)expolosionRange)
			{
				attackInfo.force = (Player.instance.tf.position - new Vector3(tf.position.x, tf.position.y - 1f, 0f)).normalized * 20f;
				Player.instance.Damaged(tf.position, attackInfo);
			}
			for (int i = 0; i < AllObjectsInfo.ArrayLength; i++)
			{
				num = Vector2.Distance(base.transform.position, AllObjectsInfo.enemy[i].pos);
				if (num < AllObjectsInfo.enemy[i].hitSize + (float)expolosionRange && AllObjectsInfo.enemy[i].hp > 0f)
				{
					attackInfo.force = (AllObjectsInfo.enemy[i].pos - new Vector2(tf.position.x, tf.position.y - 1f)).normalized * 20f;
					AllObjectsInfo.SCRT_EnemyList[i].Damaged(tf.position, attackInfo);
				}
			}
		}
		catch (Exception)
		{
		}
		SR_ExplosionZone.color = new Color(SR_ExplosionZone.color.r, SR_ExplosionZone.color.g, SR_ExplosionZone.color.b, 0f);
		base.gameObject.SetActive(false);
	}

	private void OnCollisionEnter2D(Collision2D collision)
	{
		if (collision.collider.CompareTag("Floor"))
		{
			Audio.instance.sfx[13].Play();
		}
	}

	private IEnumerator ChangeColor()
	{
		sr.color = new Color(0f, 0f, 0f);
		yield return new WaitForSeconds(2.5f);
		SR_ExplosionZone.color = new Color(SR_ExplosionZone.color.r, SR_ExplosionZone.color.g, SR_ExplosionZone.color.b, 0.5f);
		bool c = false;
		float delay = 0.8f;
		while (base.gameObject.activeSelf)
		{
			c = !c;
			sr.color = new Color(c ? 1 : 0, 0f, 0f);
			Audio.instance.sfx[37].Play();
			delay *= 0.55f;
			yield return new WaitForSeconds(delay);
		}
	}

	private IEnumerator IE_DrawShadow()
	{
		while (base.gameObject.activeSelf)
		{
			TF_Shadow.position = new Vector2(tf.position.x, 0f);
			TF_Shadow.rotation = Quaternion.Euler(Vector2.zero);
			yield return new WaitForFixedUpdate();
		}
	}
}
